import { BuffBase } from "../../buff/BuffBase";
import { BuffCreator } from "../../buff/BuffCreator";
import { BuffData } from "../../buff/const/BuffData";
import { BuffFlag } from "../../buff/const/BuffType";
import { TriggerType } from "../../trigger/const/TriggerType";
import { BattleRole } from "../BattleRole";

export class RoleBuffMgr {

    buffs: BuffBase[]

    init() {
        this.buffs = []
    }

    /**判断buff是否能添加-互斥-叠加之类的 */
    addBuff(buffData: BuffData) {
        for (let i = 0; i < this.buffs.length; i++) {
            let buff = this.buffs[i];
            if (buff.buffData.buffId == buffData.buffId) {//有相同buff
                if (buff.buffData.buffFlag == BuffFlag.add) {//叠加等级，重置回合
                    buff.buffData.level += buffData.level;
                    return;
                }
            }
        }
        let buff = BuffCreator.create(buffData);
        this.buffs.push(buff)
    }

    removeBuff(buff: BuffBase) {
        let idx = this.buffs.indexOf(buff);
        if (idx != -1) {
            this.buffs.splice(idx, 1);
        }
    }

    checkRemoveBuff() {
        for (let i = 0; i < this.buffs.length; i++) {
            let buff = this.buffs[i];
            if (buff.duation <= 0) {
                this.buffs.splice(i, 1);
                i--;
            }
        }
    }

    apply(triggerType: TriggerType, target: BattleRole) {
        for (let buff of this.buffs) {
            if (buff.triggerType == triggerType) {
                buff.excute({ target: target });
            }
        }
    }
}